﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CadDotNetTools
{
    public static class LineTypeTools
    {
        /// <summary>
        /// 创建一个新的线型
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="typeName">要创建的线型名称</param>
        /// <returns>新添加的线型表记录的ObjectId</returns>
        public static ObjectId AddLineType(this Database db, string typeName)
        {
            //打开线型表
            var lt = db.LinetypeTableId.GetObject(OpenMode.ForRead) as LinetypeTable;
            //如果存在名为typeName的线型表记录
            if (!lt.Has(typeName))
            {
                lt.UpgradeOpen();    //切换线型表为写
                //新建一个线型表记录
                var ltr = new LinetypeTableRecord();
                //设置线型表记录的名称
                ltr.Name = typeName;
                //将新的线型表记录的信息添加到线型表中
                lt.Add(ltr);
                db.TransactionManager.AddNewlyCreatedDBObject(ltr, true);
                lt.DowngradeOpen();    //为了安全，将线型表的状态切换为读
            }
            return lt[typeName];    //返回新添加的线型表记录的ObjectId
        }
        //               ltr.AsciiDescription = "刚刚加载的！";
        /// <summary>
        /// 创建一个新的线型
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="typeName">要创建的线型名称</param>
        /// <param name="typeDescription">线型说明</param>
        /// <param name="patternLEngth">线型的总长度</param>
        /// <param name="numDashes">组成线型的笔画数目</param>
        /// <returns>新添加的线型表记录的ObjectId</returns>
        public static ObjectId AddLineType(this Database db, string typeName, string typeDescription, double patternLEngth, int numDashes)
        {
            //打开线型表
            var lt = db.LinetypeTableId.GetObject(OpenMode.ForRead) as LinetypeTable;
            //如果存在名为typeName的线型表记录
            if (!lt.Has(typeName))
            {
                lt.UpgradeOpen();    //切换线型表为写
                //新建一个线型表记录
                var ltr = new LinetypeTableRecord();
                //设置线型表记录的名称
                ltr.Name = typeName;
                ltr.AsciiDescription = typeDescription;//线型说明
                ltr.PatternLength = patternLEngth; //线型的总长度
                ltr.NumDashes = numDashes; //组成线型的笔画数目

                //将新的线型表记录的信息添加到线型表中
                lt.Add(ltr);
                db.TransactionManager.AddNewlyCreatedDBObject(ltr, true);
                lt.DowngradeOpen();    //为了安全，将线型表的状态切换为读
            }
            return lt[typeName];    //返回新添加的线型表记录的ObjectId
        }
        /// <summary>
        /// 从“acadiso.lin”线型文件中装载指定线型
        /// var ltId = doc.Database.AddLineType("DashLines", "这是一个线型", 1000, 6);                    doc.Database.Celtype = ltId;  // 设置当前线型
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="typeName">要加载的线型名称</param>
        /// <returns>加载的线型的ObjectId</returns>
        public static ObjectId LoadLineType(this Database db, string typeName)
        {
            //打开线型表
            var lt = db.LinetypeTableId.GetObject(OpenMode.ForRead) as LinetypeTable;
            //如果不存在名为typeName的线型
            if (!lt.Has(typeName))
            {
                //加载typeName线型
                db.LoadLineTypeFile(typeName, "acad.lin");
            }
            return lt[typeName];     //返回加载的线型的ObjectId
        }
    }
}
